{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "75417ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72f50802",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1986-01-02</th>\n",
       "      <td>25.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-03</th>\n",
       "      <td>26.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-06</th>\n",
       "      <td>26.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-07</th>\n",
       "      <td>25.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-08</th>\n",
       "      <td>25.87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Price\n",
       "Date             \n",
       "1986-01-02  25.56\n",
       "1986-01-03  26.00\n",
       "1986-01-06  26.53\n",
       "1986-01-07  25.85\n",
       "1986-01-08  25.87"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Import the wti-daily.csv file into a data frame, in which the Date column is both\n",
    "# treated as a datetime value, and is set to be the index.\n",
    "\n",
    "filename = '../data/wti-daily.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                parse_dates=['Date'],\n",
    "                index_col=['Date'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c5c7133",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Price    22.384545\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What was the average price of a barrel of oil in June, 1992?\n",
    "df.loc['1992-06'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4918aeb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Price    19.200512\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What was the average price of a barrel of oil in all of 1987?\n",
    "df.loc['1987'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6f24a699",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Price    76.457834\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What was the average price from September 2003 through July 2014?\n",
    "df.loc['2003-09':'2014-07'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "022a0b54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1986-03-31</th>\n",
       "      <td>10.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-06-30</th>\n",
       "      <td>12.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-09-30</th>\n",
       "      <td>14.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-12-31</th>\n",
       "      <td>17.93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1987-03-31</th>\n",
       "      <td>18.82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-30</th>\n",
       "      <td>40.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>48.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-31</th>\n",
       "      <td>59.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-30</th>\n",
       "      <td>73.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-30</th>\n",
       "      <td>75.22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>99 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Price\n",
       "Date             \n",
       "1986-03-31  10.25\n",
       "1986-06-30  12.80\n",
       "1986-09-30  14.70\n",
       "1986-12-31  17.93\n",
       "1987-03-31  18.82\n",
       "...           ...\n",
       "2020-09-30  40.05\n",
       "2020-12-31  48.35\n",
       "2021-03-31  59.19\n",
       "2021-06-30  73.52\n",
       "2021-09-30  75.22\n",
       "\n",
       "[99 rows x 1 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Show the price of oil at the end of each quarter in the data set\n",
    "df[df.index.is_quarter_end]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "534d08af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1986-12-31</th>\n",
       "      <td>15.047689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1987-12-31</th>\n",
       "      <td>19.200512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1988-12-31</th>\n",
       "      <td>15.965409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1989-12-31</th>\n",
       "      <td>19.635486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-31</th>\n",
       "      <td>24.526576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1991-12-31</th>\n",
       "      <td>21.541367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1992-12-31</th>\n",
       "      <td>20.575564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1993-12-31</th>\n",
       "      <td>18.432200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1994-12-31</th>\n",
       "      <td>17.196429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>18.428805</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>22.119173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>20.608254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>14.422072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>19.344980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>30.378520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>25.983120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>26.184960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>31.075240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>41.506024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>56.637251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>66.054659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>72.340595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>99.671502</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>61.950437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>79.475714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>94.880873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>94.053333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>97.982540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>93.172222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>48.656706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>43.293651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>50.800320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>65.227470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>56.988320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>39.160437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>67.917119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Price\n",
       "Date                 \n",
       "1986-12-31  15.047689\n",
       "1987-12-31  19.200512\n",
       "1988-12-31  15.965409\n",
       "1989-12-31  19.635486\n",
       "1990-12-31  24.526576\n",
       "1991-12-31  21.541367\n",
       "1992-12-31  20.575564\n",
       "1993-12-31  18.432200\n",
       "1994-12-31  17.196429\n",
       "1995-12-31  18.428805\n",
       "1996-12-31  22.119173\n",
       "1997-12-31  20.608254\n",
       "1998-12-31  14.422072\n",
       "1999-12-31  19.344980\n",
       "2000-12-31  30.378520\n",
       "2001-12-31  25.983120\n",
       "2002-12-31  26.184960\n",
       "2003-12-31  31.075240\n",
       "2004-12-31  41.506024\n",
       "2005-12-31  56.637251\n",
       "2006-12-31  66.054659\n",
       "2007-12-31  72.340595\n",
       "2008-12-31  99.671502\n",
       "2009-12-31  61.950437\n",
       "2010-12-31  79.475714\n",
       "2011-12-31  94.880873\n",
       "2012-12-31  94.053333\n",
       "2013-12-31  97.982540\n",
       "2014-12-31  93.172222\n",
       "2015-12-31  48.656706\n",
       "2016-12-31  43.293651\n",
       "2017-12-31  50.800320\n",
       "2018-12-31  65.227470\n",
       "2019-12-31  56.988320\n",
       "2020-12-31  39.160437\n",
       "2021-12-31  67.917119"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# For each year in the data set, show the average price\n",
    "df.resample('1Y').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a397028a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2020-04-20    -36.98\n",
       "2008-07-03    145.31\n",
       "Name: Price, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# On which date were oil prices the highest? When were they the lowest?\n",
    "df['Price'].sort_values().iloc[[0, -1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0552ed57",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "idxmin   2020-04-20\n",
       "idxmax   2008-07-03\n",
       "Name: Price, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Price'].agg(['idxmin', 'idxmax'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f49554b1-dd9a-4a9f-b66f-1ed580c5d840",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
